//
//  ReturnExpressViewController.m
//  TengyunStation
//
//  Created by Johnny on 15/8/27.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "ReturnExpressViewController.h"
#import "ReturnDetailViewController.h"
#import "ExpressDetailViewController.h"
#import "ReturnAlertView.h"
#import "WaitReturnExpress.h"
#import "ReturnCell.h"

@interface ReturnExpressViewController ()<ReturnAlertViewDelegate,ReturnDetailViewControllerDelegate> {
    NSMutableArray *_expressList;
    BOOL _isReturned;
}

@end

@implementation ReturnExpressViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"退件";
        _expressList = [NSMutableArray arrayWithCapacity:1];
        _isReturned = NO;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self rightBarButtonItemTitle:@"手动输入" Method:@selector(manualInput)];

    [self reloadNumberLabelWithNumber:0];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [_scanView startScanning];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [_scanView stopScanning];
}

- (void)manualInput {
    [_scanView stopScanning];
    ReturnAlertView *alert = [[ReturnAlertView alloc] initWithDelegate:self];
    alert.codeList = _scanView.codeList;
    [alert show];
}

//刷新已接收多少件
- (void)reloadNumberLabelWithNumber:(NSInteger)number {
    NSString *numberString = [NSString stringWithFormat:@"%d",(int)number];
    NSString *string = [NSString stringWithFormat:@"已退件 %@ 件",numberString];
    NSRange range = [string rangeOfString:numberString];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
    [attrString addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:range];
    _numberLabel.attributedText = attrString;
}

#pragma mark ScanViewDelegate
- (void)didFinishScanCode:(NSString *)code {
    [_scanView stopScanning];
    ReturnDetailViewController *returnDVC = [[ReturnDetailViewController alloc] initWithNibName:@"ReturnDetailViewController" bundle:nil];
    returnDVC.expressId = code;
    returnDVC.expresser = self.expresser;
    returnDVC.delegate = self;
    [self.navigationController pushViewController:returnDVC animated:YES];
}

#pragma mark 
- (void)cancelReturnAlertInput {
    [_scanView startScanning];
}

- (void)sureReturnAlertViewExpressId:(NSString *)expressId {
    ReturnDetailViewController *returnDVC = [[ReturnDetailViewController alloc] initWithNibName:@"ReturnDetailViewController" bundle:nil];
    returnDVC.expressId = expressId;
    returnDVC.expresser = self.expresser;
    returnDVC.delegate = self;
    [self.navigationController pushViewController:returnDVC animated:YES];
}

#pragma mark ReturnDetailViewControllerDelegate
- (void)successReturnExpressWithWaitReturnExpress:(WaitReturnExpress *)express {
    if (!_isReturned) {
        _isReturned = YES;
        NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
        [mutableArray removeObjectAtIndex:1];
        self.navigationController.viewControllers = mutableArray;
    }
    [_expressList insertObject:express atIndex:0];
    [self reloadNumberLabelWithNumber:_expressList.count];
    [self.tableView reloadData];
}

#pragma mark UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _expressList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIndentifier = @"ReturnCell";
    ReturnCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ReturnCell" owner:self options:nil] lastObject];
    }
    WaitReturnExpress *express = [_expressList objectAtIndex:indexPath.row];
    cell.expressIdLabel.text = [NSString stringWithFormat:@"运单号:%@",express.expressId];
    cell.phoneLabel.text = express.telephone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 48.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 48.0)];
    footerView.backgroundColor = [UIColor clearColor];
    return footerView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WaitReturnExpress *express = [_expressList objectAtIndex:indexPath.row];
    ExpressDetailViewController *expressDVC = [[ExpressDetailViewController alloc] initWithNibName:@"ExpressDetailViewController" bundle:nil];
    expressDVC.inwayExpressId = express.inwayExpressId;
    [self.navigationController pushViewController:expressDVC animated:YES];
}

- (void)dealloc {
    _expressList = nil;
    self.expresser = nil;
}


@end
